package com.un.ebs.contract.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.un.ebs.contract.domain.Contract;
import com.un.ebs.contract.domain.ContractExt;
import com.un.ebs.contract.domain.ContractItem;
import com.un.ebs.contract.domain.ContractItemExt;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.test.context.jdbc.Sql;

import java.util.List;

/**
 * (Contract)表数据库访问层
 *
 * @author liubo
 * @since 2020-07-05 18:05:55
 */
public interface ContractMapper extends BaseMapper<Contract> {

//    @Select("select count(*) from tb_contract where code=#{code} and is_delete=0")
//    int selectCount(@Param("code") String code);

    @Select("select max(ver) from tb_contract where code=#{code} and is_delete=0")
    Integer selectMaxVer(@Param("code") String code);

    //@Select("SELECT isnull(MAX(SUBSTRING(code,LEN(code)-3,LEN(code))),0)+1 FROM dbo.tb_contract  WHERE SUBSTRING(code,1,LEN(code)-4)=#{prefix} AND type in (0,1,2) AND is_temp=1 AND is_delete=0")
    @Select("SELECT isnull(MAX(SUBSTRING(code,LEN(code)-3,LEN(code))),0)+1 FROM dbo.tb_contract  WHERE SUBSTRING(code,1,LEN(code)-4)=#{prefix}  AND is_temp=1 AND is_delete=0")
    int selectTempSaleMaxSeq(@Param("prefix") String prefix, @Param("type") int type);

    @Select("select count(*) from tb_contract where is_delete=0 and code like (#{code})")
    int selectMaxSeq(@Param("code") String code);

    @Select("SELECT isnull(MAX(SUBSTRING(code,LEN(code)-3,LEN(code))),0)+1 FROM dbo.tb_contract  WHERE SUBSTRING(code,1,LEN(code)-4)=#{prefix}  AND is_temp=0 AND is_delete=0")
    int selectFormalSaleMaxSeq(@Param("prefix") String prefix, @Param("type") int type);

    @SqlParser(filter = true)
    @Select("select * from tb_contract where id = #{id}")
    Contract selectNotTenant(@Param("id") String id);

    @SqlParser(filter = true)
    @Select("select * from v_contract_ext where id = #{id}")
    ContractExt selectExtNotTenant(@Param("id") String id);

    @SqlParser(filter = true)
    @Update("update tb_contract set workflow_pass_status=1 where id = #{id}")
    void updateWorkflowPassStatus(@Param("id") String id);

    @SqlParser(filter = true)
    @Select("select * from v_contract_item_ext where is_delete=0 and submit_status=1 and audit_status=1 and is_frame=1 and type=1 and business_ent_id=#{customerEntId} and sku_id=#{skuId}")
    ContractItemExt selectSalePrice(String customerEntId, String skuId);

    @SqlParser(filter = true)
    @Select("select * from v_contract_item_ext where is_delete=0 and submit_status=1 and audit_status=1 and is_frame=1 and type=1 and business_ent_id=#{customerEntId} and sku_code=#{skuCode}")
    ContractItemExt selectSalePriceBySkuCode(String customerEntId, String skuCode);

    @SqlParser(filter = true)
    @Select("select * from v_contract_item_ext where is_delete=0 and submit_status=1 and audit_status=1 and is_frame=1 and type=1 and sku_code=#{skuCode}")
    List<ContractItemExt> selectSalePriceBySkuCode(String skuCode);

    @SqlParser(filter = true)
    @Select("select * from v_contract_item_ext where is_delete=0 and submit_status=1 and audit_status=1 and is_frame=1 and type=0 and sku_id=#{skuId} order by price")
    List<ContractItemExt> selectPurchasePrice(String skuId);

    @Select("select * from v_contract_item_ext where contract_code=#{code} and ver=#{ver}")
    List<ContractItemExt> selectItemByContractVer(@Param("code") String code, @Param("ver") int ver);

    @SqlParser(filter = true)
    @Select("select * from v_contract_item_ext where contract_id=#{contractId} and is_delete=0 order by sku_code")
    List<ContractItemExt> selectItemExtNotTenant(@Param("contractId") String contractId);
}